using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyHealthControllerBase : HealthControllerBase
{
    public bool canBlink = true;
    public int blinkCount;
    public float blinkContinueTime;
    public float blinkIntervelTime;
    public Color blinkColor;
    public EnemyBase enemyBase;

    protected Color originalColor;
    protected SpriteRenderer spriteRenderer;
    public virtual void Awake()
    {
        enemyBase = GetComponent<EnemyBase>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }
    protected virtual void Start()
    {
        health = enemyBase.data.MaxHealth;
        CheckHealthState(health);
    }
    private void Update()
    {
        if (!canBlink)
        {
            enemyBase.controller.SetVel(enemyBase.data.HitMoveSpeed);
        }
    }
    public override void ChangeMaxHealth(int changeValue)
    {
        enemyBase.data.MaxHealth = Mathf.Max(1, enemyBase.data.MaxHealth - changeValue);
        health = Mathf.Min(health, enemyBase.data.MaxHealth);
        CheckHealthState(health);
    }
    public override void Hit(float damage)
    {
        if (damage == 0 || enemyBase.controller.GetCurState() == EnemyStateType.Null) return;
        damage = enemyBase.data.GetReduceDamageScale() * damage;
        changeValueCache -= damage;
        int value = (int)changeValueCache;
        changeValueCache -= value;
        UISystem.Instance.PopupText(enemyBase.transform.position + new Vector3(0, 0, 0), Mathf.Abs(value), null);
        health = Mathf.Clamp(health + value, 0, enemyBase.data.MaxHealth);
        CheckHealthState(health);
        if (canBlink)
        {
            canBlink = false;
            StartCoroutine(HitBlink_Coro());
            //enemyBase.controller.TransilateType(EnemyState.Hit);
        }
    }
    public override void Recover(float hp)
    {
        if (hp == 0 || enemyBase.controller.GetCurState() == EnemyStateType.Null) return;
        changeValueCache += hp;
        int value = (int)changeValueCache;
        changeValueCache -= value;
        health = Mathf.Clamp(health + value, 0, enemyBase.data.MaxHealth);
        CheckHealthState(health);
    }
    public override void DestroyForHealthZero()
    {
        enemyBase.curRoom.RemoveEnemy(gameObject);
        Destroy(gameObject);
    }
    public override void CheckHealthState(int _)
    {
        if (health == enemyBase.data.MaxHealth)
        {
            healthState = HealthState.Full;
        }
        else if (health == 0)
        {
            healthState = HealthState.Dead;
            enemyBase.controller.TransilateType(EnemyStateType.Death);
        }
        else
        {
            healthState = HealthState.Hurt;
        }
    }
    IEnumerator HitBlink_Coro()
    {
        originalColor = spriteRenderer.color;
        for (int i = 0; i < blinkCount; i++)
        {
            spriteRenderer.color = originalColor;
            for (int k = 0; k < blinkContinueTime / blinkIntervelTime; k++)
            {
                spriteRenderer.color = Color.Lerp(originalColor, blinkColor, k / (blinkContinueTime / blinkIntervelTime));
                yield return new WaitForSeconds(blinkIntervelTime);
            }
        }
        canBlink = true;
        spriteRenderer.color = originalColor;
        //enemyBase.controller.TransilateType(EnemyState.Idle);
    }
}
